package com.booza.model;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;

import com.booza.util.DecimalNumberUtils;

/**
 * Value object to store and retrieve information from stats table. Has all information related to user/player statistics.
 */
public class Stats implements Serializable, Comparable<Stats>
{
	/**
	 * Persistent fields.
	 */
	private Integer gameid;
	private GameType gametype;
	private Account accountByPlayer1;
	private Tournament tournament;
	private Account accountByPlayer2;
	private String game;
	private int score1;
	private int score2;
	private Date starttime;
	private Date finishtime;
	private Short gamestatus;
	private Integer wager;
	private int runout1;
	private int runout2;
	private int sevenballings2;
	private int sevenballings1;
	private int goldenbreak1;
	private int goldenbreak2;
	private int player1;
	private int player2;
	private Integer leagueid; // changes from int to Integer by Sagar for unit test fix, since league id is nullable field

	/**
	 * Below are Non persistent fields. Used only as DTO Especially, these fields are used to store player statistics of
	 * each game, so that it can be displayed on the "My Statistics" screen.
	 */
	private BigInteger totalWins = BigInteger.valueOf(0);
	private BigInteger totalLoss = BigInteger.valueOf(0);
	private BigInteger totalAbandoned = BigInteger.valueOf(0);
	private double percentWin;
	private double percentLost;
	private double percentAbandoned;
	private boolean hasWon;
	private Double points;
	private Integer position;
	private Double maxPoints;

	public Double getMaxPoints()
	{
		return maxPoints;
	}

	public void setMaxPoints(Double maxPoints)
	{
		this.maxPoints = maxPoints;
	}

	public Double getPoints()
	{
		return points;
	}

	public void setPoints(Double points)
	{
		this.points = points;
	}

	public Integer getPosition()
	{
		return position;
	}

	public void setPosition(Integer position)
	{
		this.position = position;
	}

	public boolean isHasWon()
	{
		return hasWon;
	}

	public void setHasWon(boolean hasWon)
	{
		this.hasWon = hasWon;
	}

	public double getPercentAbandoned()
	{
		return percentAbandoned;
	}

	public void setPercentAbandoned(double percentAbandoned)
	{
		this.percentAbandoned = percentAbandoned;
	}

	public double getPercentLost()
	{
		return percentLost;
	}

	public void setPercentLost(Integer percentLost)
	{
		this.percentLost = percentLost;
	}

	public double getPercentWin()
	{
		return percentWin;
	}

	public void setPercentWin(Integer percentWin)
	{
		this.percentWin = percentWin;
	}

	public BigInteger getTotalLoss()
	{
		return totalLoss;
	}

	public void setTotalLoss(BigInteger totalLoss)
	{
		this.totalLoss = totalLoss;
	}

	public BigInteger getTotalWins()
	{
		return totalWins;
	}

	public void setTotalWins(BigInteger totalWins)
	{
		this.totalWins = totalWins;
	}

	public int getPlayer1()
	{
		return player1;
	}

	public void setPlayer1(int player1)
	{
		this.player1 = player1;
	}

	public int getPlayer2()
	{
		return player2;
	}

	public void setPlayer2(int player2)
	{
		this.player2 = player2;
	}

	public int getGoldenbreak1()
	{
		return goldenbreak1;
	}

	public void setGoldenbreak1(int goldenbreak1)
	{
		this.goldenbreak1 = goldenbreak1;
	}

	public int getGoldenbreak2()
	{
		return goldenbreak2;
	}

	public void setGoldenbreak2(int goldenbreak2)
	{
		this.goldenbreak2 = goldenbreak2;
	}

	public int getRunout1()
	{
		return runout1;
	}

	public void setRunout1(int runout1)
	{
		this.runout1 = runout1;
	}

	public int getRunout2()
	{
		return runout2;
	}

	public void setRunout2(int runout2)
	{
		this.runout2 = runout2;
	}

	public int getSevenballings1()
	{
		return sevenballings1;
	}

	public void setSevenballings1(int sevenballings1)
	{
		this.sevenballings1 = sevenballings1;
	}

	public int getSevenballings2()
	{
		return sevenballings2;
	}

	public void setSevenballings2(int sevenballings2)
	{
		this.sevenballings2 = sevenballings2;
	}

	public Stats()
	{}

	public Stats(GameType gametype, Account accountByPlayer1, Tournament tournament, Account accountByPlayer2,
			String game, int score1, int score2, Integer leagueid)
	{
		this.gametype = gametype;
		this.accountByPlayer1 = accountByPlayer1;
		this.tournament = tournament;
		this.accountByPlayer2 = accountByPlayer2;
		this.game = game;
		this.score1 = score1;
		this.score2 = score2;
		this.leagueid = leagueid;
	}

	public Stats(GameType gametype, Account accountByPlayer1, Tournament tournament, Account accountByPlayer2,
			String game, int score1, int score2, Date starttime, Date finishtime, Short gamestatus, Integer wager,
			Integer leagueid)
	{
		this.gametype = gametype;
		this.accountByPlayer1 = accountByPlayer1;
		this.tournament = tournament;
		this.accountByPlayer2 = accountByPlayer2;
		this.game = game;
		this.score1 = score1;
		this.score2 = score2;
		this.starttime = starttime;
		this.finishtime = finishtime;
		this.gamestatus = gamestatus;
		this.wager = wager;
		this.leagueid = leagueid;
	}

	public Stats(int wager, Integer player1, int tid, Date finishTime, String name, boolean hasWon)
	{
		this.wager = wager;
		this.player1 = player1;
		this.finishtime = finishTime;
		this.game = name;
		this.hasWon = hasWon;
	}

	public Integer getGameid()
	{
		return this.gameid;
	}

	public void setGameid(Integer gameid)
	{
		this.gameid = gameid;
	}

	public GameType getGametype()
	{
		return this.gametype;
	}

	public void setGametype(GameType gametype)
	{
		this.gametype = gametype;
	}

	public Account getAccountByPlayer1()
	{
		return this.accountByPlayer1;
	}

	public void setAccountByPlayer1(Account accountByPlayer1)
	{
		this.accountByPlayer1 = accountByPlayer1;
	}

	public Tournament getTournament()
	{
		return this.tournament;
	}

	public void setTournament(Tournament tournament)
	{
		this.tournament = tournament;
	}

	public Account getAccountByPlayer2()
	{
		return this.accountByPlayer2;
	}

	public void setAccountByPlayer2(Account accountByPlayer2)
	{
		this.accountByPlayer2 = accountByPlayer2;
	}

	public String getGame()
	{
		return this.game;
	}

	public void setGame(String game)
	{
		this.game = game;
	}

	public int getScore1()
	{
		return this.score1;
	}

	public void setScore1(int score1)
	{
		this.score1 = score1;
	}

	public int getScore2()
	{
		return this.score2;
	}

	public void setScore2(int score2)
	{
		this.score2 = score2;
	}

	public Date getStarttime()
	{
		return this.starttime;
	}

	public void setStarttime(Date starttime)
	{
		this.starttime = starttime;
	}

	public Date getFinishtime()
	{
		return this.finishtime;
	}

	public void setFinishtime(Date finishtime)
	{
		this.finishtime = finishtime;
	}

	public Short getGamestatus()
	{
		return this.gamestatus;
	}

	public void setGamestatus(Short gamestatus)
	{
		this.gamestatus = gamestatus;
	}

	public Integer getWager()
	{
		return this.wager;
	}

	public void setWager(Integer wager)
	{
		this.wager = wager;
	}

	public BigInteger getTotalAbandoned()
	{
		return totalAbandoned;
	}

	public void setTotalAbandoned(BigInteger totalAbandoned)
	{
		this.totalAbandoned = totalAbandoned;
	}

	public void calculatePercentageStats()
	{
		double totGames = totalWins.add(totalLoss).doubleValue();
		if (totGames < 1) {
			percentWin = 0;
			percentLost = 0;
			percentAbandoned = 0;
		}
		else {			
			percentWin = (totalWins.doubleValue() / totGames) * 100.0;
			percentWin = new Double(DecimalNumberUtils.roundToDecimals(percentWin, DecimalNumberUtils.DECIMAL_PLACES))
					.doubleValue();
			percentLost = (totalLoss.doubleValue() / totGames) * 100.0;
			percentLost = new Double(DecimalNumberUtils.roundToDecimals(percentLost, DecimalNumberUtils.DECIMAL_PLACES))
					.doubleValue();
			percentAbandoned = (totalAbandoned.doubleValue() / totGames) * 100.0;
			percentAbandoned = new Double(DecimalNumberUtils.roundToDecimals(percentAbandoned,
					DecimalNumberUtils.DECIMAL_PLACES)).doubleValue();
		}
	}

	public int compareTo(Stats another)
	{
		return another.getFinishtime().compareTo(this.getFinishtime());
	}

	public Integer getLeagueid()
	{
		return leagueid;
	}

	public void setLeagueid(Integer leagueid)
	{
		this.leagueid = leagueid;
	}
}
